RM = rm
MV = mv
RMDIR   = rmdir
MKDIR   = mkdir
SUDO    = sudo
INSTALL = install
GLIB_COMPILER   = glib-compile-resources
GTK_ICON_UPDATE = gtk-update-icon-cache

RM_FLAGS      = -f -v
RMDIR_FLAGS   = --ignore-fail-on-non-empty
MKDIR_FLAGS   = -p -v
INSTALL_FLAGS = -m644 -v
INSTALL_BIN_FLAGS = -m755 -v
ADWFLAGS = $(shell pkg-config --cflags libadwaita-1)
CFLAGS   = -I. -Wall $(ADWFLAGS)
ADWLIBS  = $(shell pkg-config --libs libadwaita-1)
LIBS     = -lobject_v2 $(ADWLIBS)

TARGET = kcourse
TARGET_PATH = /usr

BIN     = bin
DESKTOP = share/applications
ICON    = share/icons/hicolor/512x512/apps
HICOLOR = share/icons/hicolor

DATA_PATH = .

RESOURCE_OBJECT = CourseResource.o
RESOURCE_SOURCE = CourseResource.c
DESKTOP_SOURCE  = kcourse.desktop
ICON_OBJECT = kcourse.png
GRESOURCE   = CourseResource.gresource.xml
UI_SOURCE   = CourseWindow.ui CourseCell.ui style.css

OBJECT = CourseObject.o  CourseApplication.o CourseWindow.o \
         CourseCell.o    DateObject.o        LabelObject.o  \
	 LineType.o      NumberObject.o      RangeObject.o  \
	 TopMarkObject.o TopMarkType.o       ValueObject.o  \
	 ValueType.o     main.o

all: $(TARGET)

run: $(TARGET)
	./$(TARGET)

install: $(TARGET) $(ICON_OBJECT) $(DESKTOP_SOURCE)
	$(MKDIR) $(MKDIR_FLAGS) $(TARGET_PATH)/$(BIN)
	$(MKDIR) $(MKDIR_FLAGS) $(TARGET_PATH)/$(DESKTOP)
	$(MKDIR) $(MKDIR_FLAGS) $(TARGET_PATH)/$(ICON)
	$(INSTALL) $(INSTALL_BIN_FLAGS) $(TARGET)         $(TARGET_PATH)/$(BIN)/$(TARGET)
	$(INSTALL) $(INSTALL_FLAGS)     $(ICON_OBJECT)    $(TARGET_PATH)/$(ICON)/$(ICON_OBJECT)
	$(INSTALL) $(INSTALL_FLAGS)     $(DESKTOP_SOURCE) $(TARGET_PATH)/$(DESKTOP)/$(DESKTOP_SOURCE)
	$(GTK_ICON_UPDATE)      $(TARGET_PATH)/$(HICOLOR)

uninstall:
	$(RM) $(RM_FLAGS) $(TARGET_PATH)/$(BIN)/$(TARGET)
	$(RM) $(RM_FLAGS) $(TARGET_PATH)/$(ICON)/$(ICON_OBJECT)
	$(RM) $(RM_FLAGS) $(TARGET_PATH)/$(DESKTOP)/$(DESKTOP_SOURCE)
	$(RMDIR) $(RMDIR_FLAGS) $(TARGET_PATH)/$(BIN)
	$(RMDIR) $(RMDIR_FLAGS) $(TARGET_PATH)/$(ICON)
	$(RMDIR) $(RMDIR_FLAGS) $(TARGET_PATH)/$(HICOLOR)
	$(RMDIR) $(RMDIR_FLAGS) $(TARGET_PATH)/$(DESKTOP)

$(TARGET): $(RESOURCE_OBJECT) $(OBJECT) 
	$(CC) -o $@ $^ $(LIBS)

$(OBJECT): %.o : %.c
	$(CC) -c -o $@ $< $(CFLAGS) -I$(DATA_PATH)

$(RESOURCE_OBJECT): $(RESOURCE_SOURCE)
	$(CC) -c -o $@ $< $(CFLAGS) -I$(DATA_PATH)

$(RESOURCE_SOURCE): $(UI_SOURCE) $(GRESOURCE)
	$(GLIB_COMPILER) --sourcedir=$(DATA_PATH) --generate-source $(GRESOURCE) --target=$@

.PHONY: clean
clean:
	$(RM) $(RM_FLAGS) $(OBJECT) $(TARGET) $(RESOURCE_OBJECT) $(RESOURCE_SOURCE)
